package com.tz.dream.architect.mvp.framework.lce.impl;

import android.view.View;

import com.tz.dream.architect.mvp.R;
import com.tz.dream.architect.mvp.framework.MvpActivity;
import com.tz.dream.architect.mvp.framework.MvpPresenter;
import com.tz.dream.architect.mvp.framework.MvpView;
import com.tz.dream.architect.mvp.framework.lce.ILceAnimator;
import com.tz.dream.architect.mvp.framework.lce.MvpLceView;

/**
 * 作者: Dream on 2018/3/19 21:20
 * QQ:510278658
 * E-mail:510278658@qq.com
 */

//代理模式->代理对象
public abstract class MvpLceActivity<D, V extends MvpView, P extends MvpPresenter<V>>
        extends MvpActivity<V, P> implements MvpLceView<D> {

    private MvpLceViewImpl<D> mvpLceView;

    public MvpLceViewImpl<D> getMvpLceView() {
        if (this.mvpLceView == null){
            this.mvpLceView = new MvpLceViewImpl<D>();
        }
        return mvpLceView;
    }

    //LCE加载
    @Override
    public void onContentChanged() {
        super.onContentChanged();
        getMvpLceView().initView(findViewById(R.id.rootView));
        getMvpLceView().setOnClickErrorListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onErrorClick(v);
            }
        });
    }

    public void onErrorClick(View v){
        //重新加载
        loadData(false);
    }

    public void setLceAnimator(ILceAnimator animator){
        getMvpLceView().setAnimator(animator);
    }

    @Override
    public void loadData(boolean isPullToRefresh) {
        getMvpLceView().loadData(isPullToRefresh);
    }

    @Override
    public void showLoading(boolean isPullToRefresh) {
        getMvpLceView().showLoading(isPullToRefresh);
    }

    @Override
    public void showContent() {
        getMvpLceView().showContent();
    }

    @Override
    public void bindData(D data) {
        getMvpLceView().bindData(data);
    }

    @Override
    public void showError(Throwable e, boolean pullToRefresh) {
        getMvpLceView().showError(e, pullToRefresh);
    }

}
